﻿Imports DTO
Imports BUS
Imports DevExpress.XtraEditors

Public Class frmThemKhoHang

    Public kt As Integer = 0

    Public makh As String
    Public tenkh As String
    Public nlh As String
    Public dt As String
    Public dc As String
    Public kh As String
    Public dg As String
    Public matt As Integer


    Private Sub LoadCbxNhanVien()
        cbxThemNQLKhoHang.Properties.DataSource = NhanVienBUS.LayDSNhanVienCBX()
        cbxThemNQLKhoHang.Properties.ValueMember = "MaNhanVien"
        cbxThemNQLKhoHang.Properties.DisplayMember = "TenNhanVien"
    End Sub

    Sub ThemKhoHang()
        Dim khohang As New KhoHangDTO

        khohang.KyHieu = txtThemKyHieuKhoHang.Text.Trim()
        khohang.TenKhoHang = txtThemTenKhoHang.Text.Trim()
        If cbxThemNQLKhoHang.Text.Trim() = "" Then
            khohang.MaNguoiQL = Nothing
        Else
            khohang.MaNguoiQL = cbxThemNQLKhoHang.EditValue.ToString()
        End If
        khohang.NguoiLienHe = txtThemNLHKhoHang.Text.Trim()
        khohang.DiaChi = txtThemDiaChiKhoHang.Text.Trim()
        khohang.DienThoai = txtThemDienThoaiKhoHang.Text.Trim()
        khohang.Fax = txtThemFaxKhoHang.Text.Trim()
        khohang.Email = txtThemEmailKhoHang.Text.Trim()
        khohang.DienGia = txtThemDienGiaiKhoHang.Text.Trim()
        If chkThemQuanLyKhoHang.Checked = True Then
            khohang.MaTrangThai = 1
        Else
            khohang.MaTrangThai = 0
        End If

        If KhoHangBUS.ThemKhoHang(khohang) Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Thêm thành công kho hàng", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Tên kho hàng không được để trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Sub CapNhatKhoHang()
        Dim khohang As New KhoHangDTO

        khohang.MaKhoHang = txtThemMaKhoHang.Text.Trim()
        khohang.KyHieu = txtThemKyHieuKhoHang.Text.Trim()
        khohang.TenKhoHang = txtThemTenKhoHang.Text.Trim()
        If cbxThemNQLKhoHang.Text.Trim() = "" Then
            khohang.MaNguoiQL = Nothing
        Else
            khohang.MaNguoiQL = cbxThemNQLKhoHang.EditValue.ToString()
        End If
        khohang.NguoiLienHe = txtThemNLHKhoHang.Text.Trim()
        khohang.DiaChi = txtThemDiaChiKhoHang.Text.Trim()
        khohang.DienThoai = txtThemDienThoaiKhoHang.Text.Trim()
        khohang.Fax = txtThemFaxKhoHang.Text.Trim()
        khohang.Email = txtThemEmailKhoHang.Text.Trim()
        khohang.DienGia = txtThemDienGiaiKhoHang.Text.Trim()
        If chkThemQuanLyKhoHang.Checked = True Then
            khohang.MaTrangThai = 1
        Else
            khohang.MaTrangThai = 0
        End If

        If KhoHangBUS.CapNhatKhoHang(khohang) Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Cập nhật thành công kho hàng", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Tên kho hàng không được để trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub frmThemKH_btnThemKhoHang_Click(sender As Object, e As EventArgs) Handles frmThemKH_btnThemKhoHang.Click
        If kt = 1 Then
            CapNhatKhoHang()
        Else
            ThemKhoHang()
        End If
    End Sub

    Private Sub frmThemKH_btnDongKhoHang_Click(sender As Object, e As EventArgs) Handles frmThemKH_btnDongKhoHang.Click
        Me.Close()
    End Sub

    Private Sub frmThemKhoHang_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadCbxNhanVien()
        If kt = 1 Then
            txtThemMaKhoHang.Text = makh
            txtThemTenKhoHang.Text = tenkh
            txtThemDiaChiKhoHang.Text = dc
            txtThemDienGiaiKhoHang.Text = dg
            txtThemDienThoaiKhoHang.Text = dt
            txtThemKyHieuKhoHang.Text = kh
            txtThemNLHKhoHang.Text = nlh
            If matt = 1 Then
                chkThemQuanLyKhoHang.Checked = True
            Else
                chkThemQuanLyKhoHang.Checked = False
            End If
        End If
    End Sub
End Class